<?php
namespace Home\Controller;

use Think\Controller;

class TaobaoController extends Controller
{
    public function index()
    {
        //商品搜索
        // $data = $this->getGoodsInfo('电脑');

        /**淘抢购*/
        //$data = $this->getTqgGetRequest();

        /**选品库  由选品id 获取相应选品库*/
        $res      = $this->getFavoritesGetRequest();
        $favidarr = ($res['results']['tbk_favorites']);

        foreach ($favidarr as $k => $v) {
            P($v['favorites_id']);
            $data[$v['favorites_title']] = $this->getFavoritesItemGetRequest($v['favorites_id']);
        }

        P($data);

    }

    /**  淘宝客商品查询  适用于搜索 及分类查询*/
    public function getGoodsInfo($goodsname)
    {
        $Api  = M('api');
        $conf = $Api->find(1);

        $tbk            = new \Org\Tbk\Tbk;
        $data['fields'] = "{$conf['fields']}";
        $data['name']   = "$goodsname"; //商品名称
        $data['city']   = "{$conf['itemloc']}"; //所在城市
        $data['order']  = "{$conf['sort']}"; //排序 (需要参照阿里api文档文章说明 )
        $data['tmall']  = "false"; //是否商城商品 false 为不判断
        $data['over']   = "false"; //是否海外商品
        $data['sprice'] = "{$conf['start_price']}"; // 折扣价范围下限
        $data['eprice'] = "{$conf['end_price']}"; //折扣价范围上限
        $data['tkup']   = "{$conf['start_tk_rate']}"; //淘客佣金比率上限，如：1234表示12.34%
        $data['tkdn']   = "{$conf['end_tk_rate']}"; //淘客佣金比率下限，如：1234表示12.34%
        $data['form']   = '1'; //链接形式：1：PC，2：无线，默认：１
        $data['pgno']   = "{$conf['page_no']}"; //第几页，默认：１
        $data['pgsice'] = "{$conf['page_size']}"; //页大小，默认20，1~100
        return $tbk->getGoodsRequest($data);
    }

    /** 淘宝客商品id 关联推荐查询 */
    public function getCommendGoods($name)
    {
        $tbk              = new \Org\Tbk\Tbk;
        $data['field']    = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url";
        $data['numid']    = "{$name}"; //被关联的商品Id
        $data['count']    = "8"; //返回数量，默认20，最大值40
        $data['platform'] = '1'; // 链接形式：1：PC，2：无线，默认：１
        return $tbk->getRecommendGetRequest($data);
    }

    /** 淘宝客商品详情（简版）*/
    public function getItemInfo()
    {
        $tbk              = new \Org\Tbk\Tbk;
        $data['field']    = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url";
        $data['platform'] = "1";
        $data['numid']    = "529346189421"; //被关联的商品id
        return $tbk->getItemInfoGetRequest($data);
    }

    /** 淘抢购 */
    public function getTqgGetRequest()
    {
        $Api              = M('api');
        $conf             = $Api->find(2);
        $tbk              = new \Org\Tbk\Tbk;
        $data['adzoneId'] = "{$conf['adzoneid']}"; //必填
        $data['fields']   = "click_url,pic_url,reserve_price,zk_final_price,total_amount,sold_num,title,category_name,start_time,end_time";
        $data['stime']    = "{$conf['start_time']}";
        $data['etime']    = "{$conf['end_time']}";
        $data['pageno']   = "{$conf['page_no']}";
        $data['pagesize'] = "{$conf['page_size']}"; //页大小，默认96，1~96
        return $tbk->getJuTqgGetRequest($data);
    }

    /**接选品库， 得到选品 id  */
    public function getFavoritesGetRequest()
    {
        $tbk              = new \Org\Tbk\Tbk;
        $data['fields']   = "favorites_title,favorites_id,type";
        $data['pageno']   = "1";
        $data['pagesize'] = "20";
        $data['type']     = "1"; //默认值-1；选品库类型，1：普通选品组，2：高佣选品组，-1，同时输出所有类型的选品组
        return $tbk->getUatmFavoritesGetRequest($data);
    }

    /**由 选品id 获得商品列表*/
    public function getFavoritesItemGetRequest($favorid,$fields)
    {
        
        $Api              = M('api');
        $conf             = $Api->find(3);
        $tbk              = new \Org\Tbk\Tbk;
        $data['fields']   = $fields;
        $data['adzoneid'] = "{$conf['adzoneid']}";
        $data['favorid']  = $favorid;
        $data['pageno']   = "{$conf['page_no']}";
        $data['pagesize'] = "{$conf['page_size']}";
        $data['platform'] = "1";
        return $tbk->getUatmFavoritesItemGetRequest($data);
    }

}
